package com.maomao.aop.proxy;

import com.maomao.aop.monitor.MonitorSession;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * Created by admin on 2018/3/21.
 */
public class DynamicProxy implements InvocationHandler {

    private Object target;
    public DynamicProxy(Object object){
        this.target=object;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        MonitorSession.begin(method.getName());
        Object obj=method.invoke(target,args);
        MonitorSession.end();
        return obj;
    }

    public <T> T getProxy(){
        return (T) Proxy.newProxyInstance(
                target.getClass().getClassLoader(),
                target.getClass().getInterfaces(),
                this
        );
    }
}
